<?php

class TeknisController extends Controller
{
    public $layout='//layouts/column2';
    public $defaultAction = 'admin';

    protected function getIndexList($model) {
        $indexList = array(
            array(
		'name' => 'nama',
		'value' => '($data->teknis) ? $data->teknis->keterangan : "-"',
	),array(
		'name' => 'status_kelengkapan',
                'value'=>'($data->status_kelengkapan) ? $data->status_kelengkapan : "-"'
	),array(
                'htmlOptions' => array('nowrap'=>'nowrap','class'=>'button-column'),
                'class'=>'booster.widgets.TbButtonColumn',
                'template'=>'{view}',
                'buttons'=>array(
                    'view'=>array(
                        'visible'=>'(!Yii::app()->user->isGuest && (!empty($data->upload_dokumen))) ? true : false',
                        'url'=>'array("checklistTeknis/detail/".$data->id)',
                    ),
                )
            ));
        return $indexList;
    }

    protected function getViewList($model) {
        $viewList = array(
            array(
                    'name' => 'nama_perusahaan',
                    'value' => ($model->perusahaan) ? $model->perusahaan->nama_perusahaan : "-",
            ),array(
                    'name' => 'nama',
                    'value' => ($model->teknis) ? $model->nama : "-",
            ),array(
                    'name' => 'keterangan',
                    'value' => ($model->keterangan) ? $model->keterangan : "-",
            ),array(
                    'name' => 'created',
                    'value' => ($model->created) ? $model->created : "-",
            ),array(
                    'name' => 'modified',
                    'value' => ($model->modified) ? $model->modified : "-",
            ),        );
                    return $viewList;
    }
    
//    public function actions()
//    {
//        return array(
//            'upload'=>array(
//                'class'=>'xupload.actions.XUploadAction',
//                'path' =>Yii::app()->getBasePath() . "/../uploads",
//                'publicPath' => Yii::app()->getBaseUrl() . "/uploads",
//            ),
//        );
//    }
    
    public function actionDetail($id) {
        $model=new CActiveDataProvider('ChecklistTeknis', array(
            'criteria'=>array(
                'condition'=>'izin_id='.$id,     
                ),
            'pagination'=>false,
            )
       );
        $data = Izin::model()->findByPk($id);
        $this->render('detail', array(
            'model' => $model,
            'data'=>$data,
        ));
    }

    public function actionUpdate($id)
    {
        $model= $this->loadModel($id);
        $this->performAjaxValidation($model);
        if(isset($_POST['Teknis']))
        {
            $model->attributes=$_POST['Teknis'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionCreate()
    {
        $model=new Teknis;
        $this->performAjaxValidation($model);
        if(isset($_POST['Teknis']))
        {
            $model->attributes=$_POST['Teknis'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex()
    {
        $model=new Teknis('publik');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Teknis']))
            $model->attributes=$_GET['Teknis'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionAdmin()
    {
        $model=new Teknis('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Teknis']))
            $model->attributes=$_GET['Teknis'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    public function loadModel($id)
    {
        $model=Teknis::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='teknis-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
